#include "window_common.h"
#include "gkt_door.h"

#if defined(GKT_CONFIG_DOOR_LOCK_AUTOMATIC) && GKT_CONFIG_DOOR_LOCK_AUTOMATIC

enum {
	WIDGET_ID_TITLE = 0,
	WIDGET_ID_SELECT,
	WIDGET_ID_OK_CANCEL,
	WIDGET_ID_MAX
};

enum {
	TIMER_ID_TIMEOUT,
	TIMER_ID_MAX
};

static const gkt_widget_select_attr_u sc_window_double_close_rotate_time_widget_select_attr = {
	GKT_WIDGET_SELECT_NUMBER_ATTR(GKT_DOOR_LOCK_MOTOR_ROTATE_MINTIME_MS,
			GKT_DOOR_LOCK_MOTOR_ROTATE_MAXTIME_MS,
			GKT_DOOR_LOCK_MOTOR_ROTATE_STEP_TIME_MS,
			GKT_DOOR_LOCK_DOUBLE_CLOSE_MOTOR_ROTATE_TIME_MS,
			WINDOW_TEXT_ID_msec),
};

GKT_WINDOW_WIDGET_DESCS_DEFINE(double_close_rotate_time) = {
	[WIDGET_ID_TITLE] = GKT_WIDGET_LABEL_DESC( 
		GKT_WIDGET_F_ALIGN_MIDDLE | GKT_WIDGET_F_TITLE_MARK,
		WINDOW_FONT_NORMAL_SIZE, 
		WINDOW_TITLE_FG_COLOR, WINDOW_TITLE_BG_COLOR,
		0, 0, WINDOW_WIDGET_DEFAULT_WIDTH, WINDOW_WIDGET_NORMAL_HEIGHT,
		WINDOW_TEXT_ID_double_close_rotate_time
	),
	[WIDGET_ID_SELECT] = GKT_WIDGET_SELECT_DESC(
		GKT_WIDGET_F_ALIGN_MIDDLE,
		WINDOW_FONT_LARGE_SIZE,
		WINDOW_DEFAULT_FG_COLOR, WINDOW_DEFAULT_BG_COLOR,
		0, WINDOW_WIDGET_NORMAL_HEIGHT, 
		WINDOW_WIDGET_DEFAULT_WIDTH, WINDOW_WIDGET_EXTRA_LARGE_HEIGHT,
		&sc_window_double_close_rotate_time_widget_select_attr
	),
	[WIDGET_ID_OK_CANCEL] = GKT_WIDGET_LABEL_DESC(
		GKT_WIDGET_F_ALIGN_MIDDLE,
		WINDOW_FONT_NORMAL_SIZE, 
		WINDOW_DEFAULT_FG_COLOR, WINDOW_DEFAULT_BG_COLOR,
		0, WINDOW_WIDGET_NORMAL_HEIGHT * (WINDOW_NORMAL_ROWS - 1), 
		WINDOW_WIDGET_DEFAULT_WIDTH, WINDOW_WIDGET_NORMAL_HEIGHT,
		WINDOW_TEXT_ID_cancel_ok
	),
};

typedef struct _window_double_close_rotate_time {
#if (GKT_MODULE_LOC_BACKBOARD == GKT_DOOR_LOCK_LOCATION)
	int	update_setting;
#endif
	uint32_t	time_ms;
} window_double_close_rotate_time_s;
static window_double_close_rotate_time_s s_window_double_close_rotate_time;

#if (GKT_MODULE_LOC_BACKBOARD == GKT_DOOR_LOCK_LOCATION)
#define window_double_close_rotate_time_clear_cfg_updating()	\
	do { s_window_double_close_rotate_time.update_setting = 0; } while (0)
#else
#define window_double_close_rotate_time_clear_cfg_updating()	do {} while (0)
#endif

static void window_double_close_rotate_time_complete(window_result_e result)
{
	uint32_t hint_type;

	window_double_close_rotate_time_clear_cfg_updating();

	if (WINDOW_RESULT_SET_SUCCESS == result) {
		hint_type = WINDOW_DIALOG_HINT_SUCCESS;

		gp_window_unreset_data->lock_double_close_motor_rotate_time_ms =
			s_window_double_close_rotate_time.time_ms;
		window_data_save();
	}
	else
		hint_type = WINDOW_DIALOG_HINT_FAILED;

	window_dialog_show_big_prompt(hint_type,
			WINDOW_TEXT_ID_double_close_rotate_time,
			gc_window_result_text_ids[result],
			gc_window_result_voice_ids[result]);
	gkt_window_exit();
}

static void window_double_close_rotate_time_update(void)
{
	int retval;

	retval = gkt_door_lock_set_motor_rotate_time(GKT_DOOR_LOCK_MOTOR_ROTATE_TIME_DOUBLE_CLOSE,
					s_window_double_close_rotate_time.time_ms);
	gkt_printf("window_double_close_rotate_time_update: ret=%d\n", retval);
	if (GKT_SUCCESS == retval)
		window_double_close_rotate_time_complete(WINDOW_RESULT_SET_SUCCESS);
#if (GKT_MODULE_LOC_BACKBOARD == GKT_DOOR_LOCK_LOCATION)
	else if (GKT_EINPROCESS == retval) {
		s_window_double_close_rotate_time.update_setting = 1;
		gkt_window_timer_set(TIMER_ID_TIMEOUT, 2000, 0, 0);
	}
#endif
	else
		window_double_close_rotate_time_complete(WINDOW_RESULT_SET_FAILED);
}

static int window_double_close_rotate_time_event_handler(
				uint32_t major_type, uint32_t minor_type, 
				uint32_t id, uint32_t data)
{
	int retval = GKT_EVENT_HANDLE_CONTINUE;

	switch(major_type) {
		case GKT_EVENT_WINDOW:
			if (GKT_WINDOW_INIT == minor_type) {
				window_double_close_rotate_time_clear_cfg_updating();

				s_window_double_close_rotate_time.time_ms = 
					gp_window_unreset_data->lock_double_close_motor_rotate_time_ms;
				if ((s_window_double_close_rotate_time.time_ms < GKT_DOOR_LOCK_MOTOR_ROTATE_MINTIME_MS)
					|| (s_window_double_close_rotate_time.time_ms > GKT_DOOR_LOCK_MOTOR_ROTATE_MAXTIME_MS))
				{
					s_window_double_close_rotate_time.time_ms = GKT_DOOR_LOCK_DOUBLE_CLOSE_MOTOR_ROTATE_TIME_MS;
				}
				s_window_double_close_rotate_time.time_ms = 
					((s_window_double_close_rotate_time.time_ms + GKT_DOOR_LOCK_MOTOR_ROTATE_STEP_TIME_MS - 1)
						/ GKT_DOOR_LOCK_MOTOR_ROTATE_STEP_TIME_MS) * GKT_DOOR_LOCK_MOTOR_ROTATE_STEP_TIME_MS;
			}
			else if (GKT_WINDOW_ACTIVE == minor_type) {
				window_common_mask_except_updn_cancel_ok();
				if (data) {
					gkt_widget_select_set_value(WIDGET_ID_SELECT, 
						s_window_double_close_rotate_time.time_ms);
				}
			}
			break;
		case GKT_EVENT_WIDGET:
			if (minor_type == GKT_WIDGET_EVENT_CLICK) {
				s_window_double_close_rotate_time.time_ms = 
						gkt_widget_select_get_value(WIDGET_ID_SELECT);
				window_double_close_rotate_time_update();
				return GKT_EVENT_HANDLE_COMPLETE;
			}
			break;
#if (GKT_MODULE_LOC_BACKBOARD == GKT_DOOR_LOCK_LOCATION)
		case GKT_EVENT_SYSTEM:
			if ((GKT_WINDOW_SYSTEM_DOOR == minor_type)
				&& (GKT_DOOR_NOTIFY_LOCK_CONFIG == id)
				&& s_window_double_close_rotate_time.update_setting) 
			{
				uint32_t result;
				gkt_printf("123456\n");
				result = (GKT_SUCCESS == data) ? 
					WINDOW_RESULT_SET_SUCCESS : WINDOW_RESULT_SET_FAILED;
				gkt_window_timer_clear(TIMER_ID_TIMEOUT);
				window_double_close_rotate_time_complete(result);
			}
			break;
		case GKT_EVENT_TIMER:
			if ((minor_type == TIMER_ID_TIMEOUT)
				&& s_window_double_close_rotate_time.update_setting)
			{
				window_double_close_rotate_time_complete(WINDOW_RESULT_SET_FAILED);
			}
#endif
		default:
			break;
	}

	return retval;
}
GKT_WINDOW_DESC_DEFINE(double_close_rotate_time);

#endif

